package com.example.dmweb.electricity;


import com.example.dmcommom.Enum.ElectricityCategoryEnum;
import com.example.dmcommom.NoAuthInterception;
import com.example.dmcommom.ResponseResult;
import com.example.dmcommom.SpringBeanHelper;
import com.example.dmdomain.ElectricityPriceRepository;
import com.example.dmdomain.ElectricityTimeSlotRepository;
import com.example.dmservice.excelInput.service.ExcelInPutServiceImpl;
import com.example.dmweb.electricity.model.EleNodeInfo;
import com.example.dmweb.electricity.model.ElectricityQueryResponse;
import com.example.dmweb.electricity.model.ElectricityQueryYearlyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/charge_info")
@CrossOrigin
@Api(value = "电价", tags = {"电价"})
public class ElectricityInfoController {


	ExcelInPutServiceImpl excelInPutService;

	ElectricityInfoService electricityInfoService;

	@Autowired
	public ElectricityInfoController(ExcelInPutServiceImpl excelInPutService, ElectricityInfoService electricityInfoService) {
		this.excelInPutService = excelInPutService;
		this.electricityInfoService = electricityInfoService;
	}

	@ApiOperation(value = "导入电价数据")
	@NoAuthInterception
	@RequestMapping(value = "/import", method = {RequestMethod.POST})
	public ResponseResult<String> uploadExcel(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) throws IOException {
		try {
			InputStream inputStream = file.getInputStream();
			excelInPutService.readTimeDataExcel(inputStream, type);
			return ResponseResult.success("success");
		} catch (Exception e) {
			return ResponseResult.error(e.getMessage());
		}
	}


	@ApiOperation(value = "删除电力时间段划分")
	@NoAuthInterception
	@RequestMapping(value = "/delete", method = {RequestMethod.POST})
	public ResponseResult<String> deleteData(@RequestParam(value = "province") String province,
	                                         @RequestParam(value = "city", required = false) String city) {
		ElectricityTimeSlotRepository electricityTimeSlotRepository = SpringBeanHelper.getBean(ElectricityTimeSlotRepository.class);
		if (electricityTimeSlotRepository != null) {
			electricityTimeSlotRepository.deleteAllByProvinceAndCity(province, city);
			return ResponseResult.success("删除数据成功");
		}
		return ResponseResult.error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "删除数据失败", null);
	}


	@ApiOperation(value = "查询电价数据")
	@NoAuthInterception
	@RequestMapping(value = "/query", method = {RequestMethod.GET})
	public ResponseResult<ElectricityQueryResponse> queryData(
			@RequestParam("city") String city, @RequestParam("type") String type,
			@RequestParam("voltage") String voltage, @RequestParam("year") Integer year, @RequestParam("month") Integer month) {
		ElectricityQueryResponse response;
		try {
			response = electricityInfoService.getPrice(city, type, voltage, year, month);
		} catch (Exception e) {
			return ResponseResult.error(e.getMessage());
		}
		return ResponseResult.success(response);
	}

	@ApiOperation(value = "查询全年电价数据")
	@NoAuthInterception
	@RequestMapping(value = "/queryYearly", method = {RequestMethod.GET})
	public ResponseResult<ElectricityQueryYearlyResponse> queryDataYearly(
			@RequestParam("city") String city, @RequestParam("type1") String type1, @RequestParam("type2") String type2,
			@RequestParam("voltage1") String voltage1, @RequestParam("voltage2") String voltage2,
			@RequestParam(value = "year", required = false) Integer year) {
		ElectricityQueryYearlyResponse response;
		try {
			response = electricityInfoService.getElectricityPriceLast12Month(city, type1, type2, voltage1, voltage2);
		} catch (Exception e) {
			log.info("query data failed:", e);
			return ResponseResult.error(e.getMessage());
		}
		return ResponseResult.success(response);
	}


	@ApiOperation(value = "查询全量城市数据")
	@NoAuthInterception
	@RequestMapping(value = "/queryCityInfo", method = {RequestMethod.GET})
	public ResponseResult<Map<String, List<String>>> queryCityInfo() {
		HashMap<String, List<String>> result = new HashMap<>();
		ElectricityPriceRepository electricityPriceRepository = SpringBeanHelper.getBeanOrThrow(ElectricityPriceRepository.class);
		List<String> cities = electricityPriceRepository.findAllCities();
		result.put("city", cities);
		List<String> voltages = electricityPriceRepository.findAllVoltages();
		result.put("voltage", voltages);
		List<Object[]> months = electricityPriceRepository.findAllMonths();
		List<String> mo = new ArrayList<>();
		for (Object[] month : months) {
			String str = month[0] + "年" + month[1] + "月";
			mo.add(str);
		}
		result.put("month", mo);
		List<Object[]> types = electricityPriceRepository.findAllTypes();
		List<String> typeResult = new ArrayList<>();
		for (Object[] type : types) {
			String str = ElectricityCategoryEnum.fromCode((Integer) type[0]).getName() + "_" + type[1];
			typeResult.add(str);
		}
		result.put("type", typeResult);
		return ResponseResult.success(result);
	}

	@ApiOperation(value = "查询全量城市用电制度")
	@NoAuthInterception
	@RequestMapping(value = "/queryCityEleSystem", method = {RequestMethod.GET})
	public ResponseResult<List<EleNodeInfo>> queryCityEleSystem() {
		List<EleNodeInfo> res = electricityInfoService.queryCityElectricitySystem();
		return ResponseResult.success(res);
	}
}
